﻿using Common;
using Common.Enums;
using enterprise.Website.Core.Filter;
using FreeSql.Context;
using FreeSql.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace enterprise.Website.Core.Controllers
{

    public class ManageController : Controller
    {
        private readonly SqlContext _sqlContext;
        public ManageController(SqlContext sqlContext)
        {
            _sqlContext = sqlContext;
        }


        public async Task<IActionResult> Login(string token, string email)
        {
            if (!string.IsNullOrEmpty(token))
            {
                bool isPass = await ApiLogin.TokenLogin(token, email,HttpContext.Request.Host.Host);
                if (isPass)
                {
                    var AdminInfo = await _sqlContext.QueryInfoAsync<Admin>(m => m.UserName == "admin");
                    if (AdminInfo != null)
                    {
                        var SessionBody = HttpContext.Session.GetString(SystemConstConfig.AuthHeader);
                        //如果存在则删除原Session
                        if (!SessionBody.IsNullOrWhiteSpace())
                        {
                            HttpContext.Session.Remove(SystemConstConfig.AuthHeader);
                        }
                        //增加新Session
                        HttpContext.Session.SetString(SystemConstConfig.AuthHeader, AESEncryptHelper.Encode(new UserInfo
                        {
                            Id = AdminInfo.Id,
                            UserName = AdminInfo.UserName
                        }.ToJson()));
                        //设置Lookchem后台登录Seesion
                        var lookchemLoginSeeion= HttpContext.Session.GetString(SystemConstConfig.LookchemLogin);
                        if (!lookchemLoginSeeion.IsNullOrWhiteSpace())
                        {
                            HttpContext.Session.Remove(SystemConstConfig.LookchemLogin);
                        }
                        HttpContext.Session.SetString(SystemConstConfig.LookchemLogin, token);
                        string RequestId = HttpContext.Connection.RemoteIpAddress.ToString();
                        string LoginTime = DateTime.Now.ToString();
                        //记录最后一次登录时间和Ip
                        await _sqlContext.UpdateAsync<Admin>(m => new Admin
                        {
                            LoginIp = RequestId,
                            LoginTime = LoginTime
                        }, n => n.Id == AdminInfo.Id);

                        return RedirectToAction("Index");
                    }
                }
            }
            return View();
        }

        /// <summary>
        /// 后台管理首页
        /// </summary>
        /// <returns></returns>
        [LoginAuthorize]
        [Route("manage")]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public IActionResult LoginOut()
        {
            var SessionBody = HttpContext.Session.GetString(SystemConstConfig.AuthHeader);
            //如果存在则删除原Session
            if (!SessionBody.IsNullOrWhiteSpace())
            {
                HttpContext.Session.Remove(SystemConstConfig.AuthHeader);
            }
            var tokenSession= HttpContext.Session.GetString(SystemConstConfig.LookchemLogin);
            if (!tokenSession.IsNullOrWhiteSpace())
            {
                HttpContext.Session.Remove(SystemConstConfig.LookchemLogin);
            }
            return Redirect("/manage/login");
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <returns></returns>
        public IActionResult Password()
        {
            return View();
        }

        /// <summary>
        /// 后台人员管理页面
        /// </summary>
        /// <returns></returns>

        public IActionResult AdminList()
        {
            return View();
        }

        /// <summary>
        /// 网站信息
        /// </summary>
        /// <returns></returns>
        public IActionResult WebElement()
        {
            return View();
        }

        /// <summary>
        /// 语言管理
        /// </summary>
        /// <returns></returns>
        public IActionResult LanguageType()
        {
            return View();
        }

        /// <summary>
        /// 语言标签管理
        /// </summary>
        /// <returns></returns>
        public IActionResult LanguageText()
        {
            return View();
        }

        /// <summary>
        /// 网站栏目设置
        /// </summary>
        /// <returns></returns>
        public IActionResult ContentClass()
        {
            return View();
        }

        /// <summary>
        /// 网站栏目编辑
        /// </summary>
        /// <returns></returns>
        public IActionResult ContentClassEdit(int classId = 0)
        {
            ViewData["classId"] = classId;
            return View();
        }

        /// <summary>
        /// 客户留言管理
        /// </summary>
        /// <returns></returns>
        public IActionResult MessageInfo()
        {
            return View();
        }

        /// <summary>
        /// 友情链接管理
        /// </summary>
        /// <returns></returns>
        public IActionResult FriendLink()
        {
            return View();
        }

        /// <summary>
        /// 新闻列表
        /// </summary>
        /// <returns></returns>
        public IActionResult News()
        {
            return View();
        }

        /// <summary>
        /// 新闻编辑
        /// </summary>
        /// <returns></returns>
        public IActionResult NewsEdit(int Id = 0)
        {
            ViewData["Id"] = Id;
            return View();
        }

        /// <summary>
        /// 产品管理
        /// </summary>
        /// <returns></returns>
        [Route("manage/Products")]
        public IActionResult Products()
        {
            return View();
        }

        /// <summary>
        /// 产品编辑页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult ProductEdit(int id = 0)
        {
            ViewData["id"] = id;
            return View();
        }

        /// <summary>
        /// Excel导入
        /// </summary>
        /// <returns></returns>
        public IActionResult ExcelToProduct()
        {
            return View();
        }

        /// <summary>
        /// Sql管理
        /// </summary>
        /// <returns></returns>
        [LoginAuthorize]
        public IActionResult AdminSql()
        {
            return View();
        }

        /// <summary>
        /// 首页Banner图管理
        /// </summary>
        /// <returns></returns>
        public IActionResult Banner()
        {
            return View();
        }

        /// <summary>
        /// 首页管理
        /// </summary>
        /// <returns></returns>
        public IActionResult WebHome()
        {
            return View();
        }

        /// <summary>
        /// 首页编辑
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> WebHomeEdit(int languageId = 0)
        {
            var Model = await _sqlContext.QueryInfoAsync<LanguageType>(m => m.Id == languageId);
            ViewData["languageId"] = languageId;
            return View(Model);
        }
    }
}
